/**
 * PHOSIDE: PHosphorylation Site IDentification Engine.
 * Copyright 2009 Digital Biology Lab, University of Missouri.
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version. <p/> This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more
 * details.
 */

package phoside.miscellaneous.io;

import java.io.IOException;

import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import phoside.PhosphoProteins;

import phoside.classify.result.PhosidePredictionResult;
import phoside.classify.result.PhosidePredictionResultImpl;

import phoside.io.ProteinsReader;
import phoside.io.xml.PhosideXmlProteinsReader;
import phoside.io.xml.PhosphoProteinFieldValueFormatter;

import phoside.util.IOUtil;

/**
 *
 * @author gjj
 */
public class PhosphoReaderNetPhosReport implements ProteinsReader {
    private PhosidePredictionResult data;
    private String dirReport;

    public PhosphoReaderNetPhosReport(String dirReport, PhosidePredictionResult data) {
        this.data = data;
        this.dirReport = dirReport;
    }

    public PhosphoProteins read() throws IOException {
        List<String> lines = IOUtil.readStringListAscii(dirReport);
        Pattern p = Pattern.compile("(.*)_name +([0-9]+)+ +[^ ]+ +([01]\\.[0-9]+) +(.+)");
        for (String line : lines) {
            Matcher m = p.matcher(line);
            if (m.matches()) {
                String acc = m.group(1);
                int site = Integer.parseInt(m.group(2))-1;
                double score = Double.parseDouble(m.group(3));
                if (m.group(4).startsWith("*"))
                    data.addSite(acc, site);
                data.setPrediction(acc, site, score);
            }
        }
        return data;
    }

    public static void main(String[] args) {
        String testXml = "exp\\test1\\human-y\\Human-UniProt-SwissProt-V15.8-20090922-PhosphoELM-V8.2-April2009-nr50-test-Y.xml";
        String dirReport = "exp\\test1\\human-y\\NetPhos2.0-result-human-Y.txt";
        String out = "exp\\test1\\human-y\\Netphos2.0-result-human-Y.xml";

        PhosidePredictionResult data = new PhosidePredictionResultImpl(null);
        PhosideXmlProteinsReader readerxml = new PhosideXmlProteinsReader(testXml, data,
                new PhosphoProteinFieldValueFormatter());
        PhosphoReaderNetPhosReport reader = new PhosphoReaderNetPhosReport(dirReport, data);
        phoside.io.xml.PhosideXmlProteinsWriter writer
                = new phoside.io.xml.PhosideXmlProteinsWriter(out,
                new phoside.io.xml.PhosphoProteinFieldValueFormatter());        

        try {
            readerxml.read();
            for (String acc : data.getProteinsAccessions()) {
                data.clearSites(acc);
                data.getProtein(acc).removeInfo(phoside.util.StaticFinalValues.DISORDER);
            }

            reader.read();
            writer.write(data);
        } catch(IOException e) {
            e.printStackTrace();
        }
    }
}
